package io.renren.modules.api.controller;

import cn.hutool.json.JSONUtil;
import io.renren.common.annotation.OpenApi;
import io.renren.common.utils.Constants;
import io.renren.common.utils.Result;
import io.renren.modules.seal.dao.EcifDao;
import io.renren.modules.seal.vo.EcifSealVo;
import io.renren.modules.seal.vo.EcifVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @auther xiegaolong
 * @description
 * @date 2024/12/2
 */
@RestController
@Slf4j
@RequestMapping("/a001")
public class A001Controller {

    @Resource
    private EcifDao ecifDao;


    @OpenApi
    @PostMapping("/findEcifInfo")
    public Result findEcifInfo(String custNo, String transNo){
        log.info("A001查询findEcifInfo接口，custNo:{},transNo:{}",custNo,transNo);
        EcifVo ecifInfo = ecifDao.findEcifByCifseq(custNo);
        if (Objects.isNull(ecifInfo)){
            return Result.errorMsg("企业还未认证");
        }
        if (!"3".equals(ecifInfo.getAuthStatus())){
            return Result.errorMsg("企业认证未通过");
        }
        Map<String,Object> resultMap = new HashMap<>();
        //企业信息
        Map<String,Object> ecifInfoMap = new HashMap<>();
        ecifInfoMap.put("cifseq",custNo);
        ecifInfoMap.put("cifname",ecifInfo.getCifname());
        ecifInfoMap.put("creditcode",ecifInfo.getCreditcode());
        ecifInfoMap.put("legalname",ecifInfo.getLegalname());
        ecifInfoMap.put("legalidno",ecifInfo.getLegalidno());
        ecifInfoMap.put("legalidtype",ecifInfo.getLegalidtype());

        resultMap.put("ecifInfo",ecifInfoMap);
        //印章信息
        EcifSealVo seal = ecifDao.findSealByCifseq(custNo);
        Map<String,Object> sealMap = new HashMap<>();
        Boolean isKj = Constants.isKj;
        //印章状态 0:未制作 1：启动 2：禁用
        if (seal != null && seal.getSealStatus().equals("1")){
            EcifSealVo ecifVo = new EcifSealVo();
            BeanUtils.copyProperties(seal,ecifVo);

            sealMap.put("sealCode",isKj? "Kj"+custNo:"Sy:"+custNo);
            sealMap.put("certDn",seal.getCertDn());
            sealMap.put("certSn",seal.getCertSn());
            sealMap.put("certEffectStart",seal.getCertEffectStart());
            sealMap.put("certEffectEnd",seal.getCertEffectEnd());
            sealMap.put("sealPhoto",seal.getSealPhotoUrl());
        }else{
            sealMap.put("sealCode","");
            sealMap.put("certDn","");
            sealMap.put("certSn","");
            sealMap.put("certEffectStart","");
            sealMap.put("certEffectEnd","");
            sealMap.put("sealPhoto","");
        }
        resultMap.put("sealInfo",sealMap);
        log.info("查询企业认证信息,结果:{}", JSONUtil.toJsonStr(resultMap));

        return Result.success(resultMap);
    }
}
